package com.cmpe.av.androidservice;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cmpe.av.MySQLAccess;
import com.cmpe.av.logic.UserLogic;
import com.cmpe.av.model.User;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class AndroidLogin
 */
@WebServlet("/AndroidLogin")
public class AndroidLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AndroidLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @throws IOException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.reset();
		ObjectMapper mapper = new ObjectMapper();
		PrintWriter out = response.getWriter();
		String username = request.getParameter("user");
		String pass = request.getParameter("pass");
		User user = null;
	
			try {
				
				user = new UserLogic().getUser(username, pass);
				if (user != null) {
					
					out.write(mapper.writeValueAsString(user));
				} else {
					out.write("invalid");
				}
			} catch (Exception e) {
				out.write("error");
				System.out.println(e + " android");
			}

		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
